package com.pactera.madp.cp.controller.oms.corp;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pactera.madp.common.core.exception.CheckedException;
import com.pactera.madp.common.core.util.R;
import com.pactera.madp.common.log.annotation.SysLog;
import com.pactera.madp.cp.api.dto.corp.CorpDto;
import com.pactera.madp.cp.api.entity.CorpEntity;
import com.pactera.madp.cp.api.vo.corp.CorpDetailVo;
import com.pactera.madp.cp.common.util.WxApiUtils;
import com.pactera.madp.cp.service.ICorpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @description:企业微信授权
 * @author: Huayu
 * @time: 2020/11/23 18:19
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/corp")
@Api(value = "corp", tags = "企业微信授权")
public class CorpController {

    private ICorpService corpServiceImpl;

    /**
     * 企业列表分页查询
     * @param page
     * @return
     */
    @GetMapping(value = "/page")
    @ApiOperation(value = "企业信息列表")
    public R getCorpList(Page page) {
        return corpServiceImpl.getCorpPageList(page);
    }

    /**
     * 创建企业信息
     * @param corpDto
     * @return
     * @throws Exception
     */
    @SysLog("创建企业信息")
    @PostMapping
    @ApiOperation(value = "创建企业信息")
    public R createCorp(@Validated()  @RequestBody CorpDto corpDto) {
        String contactAccess = WxApiUtils.getAccessToken(corpDto.getWxCorpId(), corpDto.getContactSecret());
        if (contactAccess == null) {
            throw new CheckedException("外部联系人秘钥错误");
        }

        String employeeAccess = WxApiUtils.getAccessToken(corpDto.getWxCorpId(), corpDto.getEmployeeSecret());
        if (employeeAccess == null) {
            throw new CheckedException("通讯录秘钥错误");
        }

        return corpServiceImpl.createCorp(corpDto);
    }

    /**
     * 更新企业信息
     * @param corpDto
     * @return
     */
    @SysLog("更新企业信息")
    @PutMapping
    @ApiOperation(value = "更新企业信息")
    public R updateCorp(@Validated()  @RequestBody CorpDto corpDto) {
        CorpEntity corpInfo = corpServiceImpl.getCorpInfoById(corpDto.getCorpId());
        if (corpInfo == null) {
            throw new CheckedException("非法参数");
        }
        CorpEntity corpEntity = new CorpEntity();
        BeanUtils.copyProperties(corpDto, corpEntity);
        corpEntity.setUpdateTime(LocalDateTime.now());
        boolean flag = corpServiceImpl.updateCorpByCorpId(corpEntity);
        if (flag) {
            log.info("企业微信授信更新成功");
        }
        return flag ? R.ok() : R.failed();
    }

    /**
     * 登录用户绑定企业信息
     * @return
     */
    @PostMapping(value = "/bind")
    @ApiOperation(value = "登录用户绑定企业信息")
    public R bindCorp() {
        return corpServiceImpl.bindCorp();
    }

    /**
     * 默认企业设置
     * @param corpId
     * @return
     */
    @SysLog("默认企业设置")
    @PutMapping("/default")
    @ApiOperation(value = "默认企业设置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "corpId", value = "企业id", dataTypeClass = Integer.class),
    })
    public R defaultCorp(@RequestParam("corpId") Integer corpId) {
        return corpServiceImpl.defaultCorp(corpId);
    }

    /**
     * @param corpId
     * @description: 企业微信授权-详情
     * @return:
     * @author: Huayu
     * @time: 2020/11/23 19:48
     */
    @GetMapping(value = "/{corpId}")
    @ApiOperation(value = "企业微信授权-详情")
    @ApiImplicitParams({@ApiImplicitParam(name="corpId",value="企业id",dataType = "Integer")})
    public R showCorp(@PathVariable Integer corpId) {
        CorpEntity corpEntity = corpServiceImpl.getCorpInfoById(corpId);
        if (corpEntity == null) {
            throw new CheckedException("企业信息不存在");
        }
        CorpDetailVo vo = new CorpDetailVo();
        BeanUtil.copyProperties(corpEntity, vo);
        log.info("企业微信授权 - 详情<<<<<<<<< {}",vo);

        return R.ok(vo);
    }

}
